package com.demomaster.quickcompat.widget.viewpager;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.demomaster.quickcompat.R;

public class QuickViewPager extends ViewPager {
    public QuickViewPager(@NonNull Context context) {
        super(context);
    }

    public QuickViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        handAttributeSet(attrs);
    }

    boolean canScroll = true;
    public void setCanScroll(boolean canScroll) {
        this.canScroll = canScroll;
    }
    private void handAttributeSet(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.QuickViewPager);
            canScroll = a.getBoolean(R.styleable.QuickViewPager_canScroll, true);
            //hide_with_child = a.getBoolean(R.styleable.VisibleLayout_hide_with_child, hide_with_child);
            a.recycle();
        }
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(canScroll) {
            final int action = ev.getAction() & MotionEvent.ACTION_MASK;
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    mActivePointerId = ev.getPointerId(0);
                    break;
                case MotionEvent.ACTION_MOVE:
                    final int activePointerId = mActivePointerId;
                    try {
                        final int pointerIndex = ev.findPointerIndex(activePointerId);
                        //QDLogger.d("onInterceptTouchEvent", "pointerIndex:" + pointerIndex);
                        if (pointerIndex == -1) {//处理viewpager -1时报错崩溃
                            return false;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (activePointerId == -1) {
                        // If we don't have a valid id, the touch down wasn't on content.
                        return false;
                    }
                    break;
            }

            return super.onInterceptTouchEvent(ev);
        }
        return false;
    }
    int mActivePointerId;
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(canScroll) {
            final int action = ev.getAction() & MotionEvent.ACTION_MASK;

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    mActivePointerId = ev.getPointerId(0);
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    return false; // 禁止多指操作
                case MotionEvent.ACTION_MOVE:
                    final int pointerIndex = ev.findPointerIndex(mActivePointerId);
                    if (pointerIndex < 0) {
                        // Invalid pointer index
                        return false;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    mActivePointerId = MotionEvent.INVALID_POINTER_ID;
                    break;
            }

            return super.onTouchEvent(ev);
        }
        return false;
    }
}
